package com.example.rocketmq.demos;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
@Slf4j
public class AsyncProducer {

    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("test-producer");
        producer.setNamesrvAddr("192.168.101.141:9876");
        try {
            producer.start();
            for (int i = 0; i < 100; i++) {
                String msg = "test message" + i;
                Message message = new Message("test-topic", "test-producer", msg.getBytes(StandardCharsets.UTF_8));
                producer.send(message, new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        log.info(sendResult.toString());

                    }

                    @Override
                    public void onException(Throwable throwable) {
                        throwable.printStackTrace();

                    }
                });
            }
        } catch (MQClientException | RemotingException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
